/*
 * Copyright 2018-10-18 13:38:10 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.utils.Constants;
import com.lap.scp.admin.utils.ExcelUtils;
import com.lap.scp.erp.ao.PurchaseOrdersAO;
import com.lap.scp.erp.domain.PurchaseOrdersDO;
import com.lap.scp.erp.query.PurchaseOrdersQueryVO;

/**
 * <pre>
 *  记录物料出入库信息控制对象，提供 记录物料出入库信息 新增,编辑,查询等操作。
 *  控制层统一命名规则：
 *  进入新增 	add,
 *  增加保存 	save,
 *  进入修改 	edit,
 *  修改保存 	update,
 *  查询分页 	queryPage,
 *  根据ID查询 	get,
 *  删除 		deleteById
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-10-18 13:38:10
 * @since 2.0.0
 */
@Controller
@RequestMapping("purchaseOrders")
public class PurchaseOrdersController {
	/**
	 * 记录物料出入库信息应用流程对象
	 */
	@Resource
	private PurchaseOrdersAO purchaseOrdersAO;

	/**
	 * <pre>
	 * 根据条件、分页信息查询记录物料出入库信息
	 * </pre>
	 * 
	 * @param page
	 * @param queryVO
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView queryPage(Page page, PurchaseOrdersQueryVO queryVO) {
		ModelAndView andView = new ModelAndView();

		PageResult<PurchaseOrdersDO> pr = purchaseOrdersAO.queryPage(page, queryVO);
		if (pr.isSuccess()) {
			andView.setViewName("erp/purchaseOrdersList");
			andView.addObject("queryVO", queryVO);
			andView.addObject("pr", pr);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * 导出数据
	 * 
	 * @param queryVO
	 * @return
	 */
	@RequestMapping(value = "export")
	@ResponseBody
	public void export(HttpServletResponse response, PurchaseOrdersQueryVO queryVO) {
		String fileName = Constants.PURCHASE_ORDERS + System.currentTimeMillis() + ".xls";
		Resp<Workbook> resp = purchaseOrdersAO.export(queryVO);
		ExcelUtils.setResponseHeader(response, fileName);
		ExcelUtils.writeExcel(resp.getData(), response);
	}

}
